/**
 * 
 */
package com.istart.webmagic.guba;

import java.util.ArrayList;
import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @author code4crafter@gmail.com <br>
 */
public class GuBaRepoPageProcessor implements PageProcessor {
	//http://guba.eastmoney.com/default_1.html
    public static final String URL_LIST = "http://guba\\.eastmoney\\.com/default_\\d+\\.html";
//    http://guba.eastmoney.com/news,gssz,257496576.html
//    http://guba.eastmoney.com/news,zsgjgdaxi,257494345.html
    public static final String URL_POST = "http://guba\\.eastmoney\\.com/news,\\w+,\\d+\\.html";

    public static final String URL_NEXT = "default_\\d+\\.html";
    private Site site = Site
            .me()
            .setDomain("guba.eastmoney.com/")
            .setSleepTime(3000)
            .setUserAgent(
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
    @Override
    public void process(Page page) {
        //列表页<a href="/news,szzs,257597308.html" title="今晚消息汇总：国务院确定一重要政策！" class="note">今晚消息汇总：国务院确定一重要政策！</a>
        if (page.getUrl().regex(URL_LIST).match()) {
            page.addTargetRequests(page.getHtml().xpath("//ul[@class=\"newlist\"]").links().regex(URL_POST).all());
            String xx = page.getHtml().xpath("//ul[@class=\"pagernums\"]").toString();
            xx = xx.replace("<ul class=\"pagernums\" data-pager=\"default_|", "");
            xx = xx.substring(0, xx.indexOf("|"));
            List<String> list = new ArrayList<>();
       	 	for (int i = 1; i < Integer.valueOf(xx) + 1; i++) {
       	 		list.add("http://guba.eastmoney.com/default_" + i + ".html");
       	 	}
            page.addTargetRequests(list);
        } else {
        	//标题
            page.putField("标题", page.getHtml().xpath("//div[@id='zwconttbt']"));
            //内容
            page.putField("内容", page.getHtml().xpath("//div[@id='zwconbody']"));
            //作者
            page.putField("作者", page.getHtml().xpath("//div[@id='zwconttbn']//a"));
            //评论数
            page.putField("评论数", page.getHtml().xpath("//div[@id='zwmbtilr']/span[@class='tc1']"));
            //阅读数
            page.putField("阅读数", page.getHtml().xpath("//span[@class='tc1']"));
            //发表日期
            //发表时间
            page.putField("发表日期时间", page.getHtml().xpath("//div[@class='zwfbtime']"));
            //最后更新时间
            page.putField("最后更新时间", page.getHtml().xpath("//div[@class='zwfbtime']"));
            //发表地方
            page.putField("发表地方",page.getHtml().xpath("//div[@class='zwfbtime']"));
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new GuBaRepoPageProcessor())
        	.addUrl("http://guba.eastmoney.com/default_1.html")
        	.addPipeline(new JsonFilePipeline("G:/webmagic/"))
        	.thread(5)
            .run();
    }
}